package com.StudentManager;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    static ArrayList<Student> list = new ArrayList<>();
    static Scanner sc = new Scanner(System.in);

    public static void start() {
        while (true) {
            System.out.println("=== 学生管理系统 ===");
            System.out.println("1. 添加学生");
            System.out.println("2. 查看学生");
            System.out.println("3. 删除学生");
            System.out.println("4. 修改学生");
            System.out.println("5. 退出系统");
            System.out.print("请输入你的选择：");
            String choice = sc.nextLine();

            switch (choice) {
                case "1": addStudent(); break;
                case "2": viewStudents(); break;
                case "3": deleteStudent(); break;
                case "4": updateStudent(); break;
                case "5": System.out.println("系统已退出！"); return;
                default: System.out.println("输入有误，请重新输入！");
            }
        }
    }

    public static void addStudent() {
        String id;
        while (true) {
            System.out.print("请输入学号：");
            id = sc.nextLine();
            if (getIndex(id) == -1) break;
            System.out.println("该学号已存在，请重新输入！");
        }
        System.out.print("请输入姓名：");
        String name = sc.nextLine();
        System.out.print("请输入年龄：");
        int age = Integer.parseInt(sc.nextLine());
        System.out.print("请输入地址：");
        String address = sc.nextLine();

        Student stu = new Student(id, name, age, address);
        list.add(stu);
        System.out.println("学生添加成功！");
    }

    public static void viewStudents() {
        if (list.isEmpty()) {
            System.out.println("暂无学生信息！");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t地址");
        for (Student stu : list) {
            System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "岁\t" + stu.getAddress());
        }
    }

    public static void deleteStudent() {
        System.out.print("请输入要删除的学生学号：");
        String id = sc.nextLine();
        int index = getIndex(id);
        if (index == -1) {
            System.out.println("该学号不存在！");
        } else {
            list.remove(index);
            System.out.println("学生删除成功！");
        }
    }

    public static void updateStudent() {
        System.out.print("请输入要修改的学生学号：");
        String id = sc.nextLine();
        int index = getIndex(id);
        if (index == -1) {
            System.out.println("该学号不存在！");
        } else {
            Student stu = list.get(index);
            System.out.print("请输入新的姓名：");
            stu.setName(sc.nextLine());
            System.out.print("请输入新的年龄：");
            stu.setAge(Integer.parseInt(sc.nextLine()));
            System.out.print("请输入新的地址：");
            stu.setAddress(sc.nextLine());
            System.out.println("学生信息修改成功！");
        }
    }

    public static int getIndex(String id) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(id)) return i;
        }
        return -1;
    }
}

